rm(list = ls())
library(foreign)
shame <- read.csv("/Users/cbrandt/Dropbox/AAA_Projects/UNSC/data/brandt_kotajoki_naming_and_shaming_at_the_UNSC.csv")
actor_id = read.csv("/Users/cbrandt/Dropbox/Data/UCDP/ucdp-actor-231.csv")
shame$govt <- ifelse(shame$condemned.ucdp.id < 160, 1, 0)

shame$any_member = 1 
#In text statistics -----
# For 'humanitarian'
agg_human <- aggregate(shame$any_member, 
                       by = list(shame$condemned.ucdp.id, shame$humanitarian, shame$doc.id), 
                       FUN = sum)
colnames(agg_human) <- c("condemned.ucdp.id", "humanitarian", "doc.id", "x")
agg_human$govt <- ifelse(agg_human$condemned.ucdp.id < 160, 1, 0)
sum(agg_human$humanitarian[agg_human$govt == 1])
sum(agg_human$humanitarian[agg_human$govt == 0])
sum(agg_human$humanitarian[agg_human$govt == 1])*100/nrow(shame[which(shame$govt==1),])
sum(agg_human$humanitarian[agg_human$govt == 0])*100/nrow(shame[which(shame$govt==0),])
t.test(agg_human$humanitarian[which(agg_human$govt == 0)],
       agg_human$humanitarian[which(agg_human$govt == 1)])


# For 'gender'
agg_gender <- aggregate(shame$any_member, 
                        by = list(shame$condemned.ucdp.id, shame$gender, shame$doc.id), 
                        FUN = sum)
colnames(agg_gender) <- c("condemned.ucdp.id", "gender", "doc.id", "x")
agg_gender$govt <- ifelse(agg_gender$condemned.ucdp.id < 160, 1, 0)
sum(agg_gender$gender[agg_gender$govt == 1])
sum(agg_gender$gender[agg_gender$govt == 0])
sum(agg_gender$gender[agg_gender$govt == 1])*100/nrow(shame[which(shame$govt==1),])
sum(agg_gender$gender[agg_gender$govt == 0])*100/nrow(shame[which(shame$govt==0),])
t.test(agg_gender$gender[which(agg_gender$govt == 0)],
       agg_gender$gender[which(agg_gender$govt == 1)])

# For 'weapons'
agg_weapons <- aggregate(shame$any_member, 
                         by = list(shame$condemned.ucdp.id, shame$weapons, shame$doc.id), 
                         FUN = sum)
colnames(agg_weapons) <- c("condemned.ucdp.id", "weapons", "doc.id", "x")
agg_weapons$govt <- ifelse(agg_weapons$condemned.ucdp.id < 160, 1, 0)
sum(agg_weapons$weapons[agg_weapons$govt == 1])
sum(agg_weapons$weapons[agg_weapons$govt == 0])
sum(agg_weapons$weapons[agg_weapons$govt == 1])*100/nrow(shame[which(shame$govt==1),])
sum(agg_weapons$weapons[agg_weapons$govt == 0])*100/nrow(shame[which(shame$govt==0),])
t.test(agg_weapons$weapons[which(agg_weapons$govt == 0)],
       agg_weapons$weapons[which(agg_weapons$govt == 1)])

# For 'violence'
agg_violence <- aggregate(shame$any_member, 
                          by = list(shame$condemned.ucdp.id, shame$violence, shame$doc.id), 
                          FUN = sum)
colnames(agg_violence) <- c("condemned.ucdp.id", "violence", "doc.id", "x")
agg_violence$govt <- ifelse(agg_violence$condemned.ucdp.id < 160, 1, 0)
sum(agg_violence$violence[agg_violence$govt == 1])
sum(agg_violence$violence[agg_violence$govt == 0])
sum(agg_violence$violence[agg_violence$govt == 1])*100/nrow(shame[which(shame$govt==1),])
sum(agg_violence$violence[agg_violence$govt == 0])*100/nrow(shame[which(shame$govt==0),])

t.test(agg_violence$violence[which(agg_violence$govt == 0)],
       agg_violence$violence[which(agg_violence$govt == 1)])

# For 'assassination'
agg_ass <- aggregate(shame$any_member, 
                     by = list(shame$condemned.ucdp.id, shame$assassination, shame$doc.id), 
                     FUN = sum)
colnames(agg_ass) <- c("condemned.ucdp.id", "assassination", "doc.id", "x")
agg_ass$govt <- ifelse(agg_ass$condemned.ucdp.id < 160, 1, 0)
sum(agg_ass$assassination[agg_ass$govt == 1])
sum(agg_ass$assassination[agg_ass$govt == 0])
sum(agg_ass$assassination[agg_ass$govt == 1])*100/nrow(shame[which(shame$govt==1),])
sum(agg_ass$assassination[agg_ass$govt == 0])*100/nrow(shame[which(shame$govt==0),])
t.test(agg_ass$assassination[which(agg_ass$govt == 0)],
       agg_ass$assassination[which(agg_ass$govt == 1)])

# For 'other'
agg_other <- aggregate(shame$any_member, 
                       by = list(shame$condemned.ucdp.id, shame$other, shame$doc.id), 
                       FUN = sum)
colnames(agg_other) <- c("condemned.ucdp.id", "other", "doc.id", "x")
agg_other$govt <- ifelse(agg_other$condemned.ucdp.id < 160, 1, 0)
sum(agg_other$other[agg_other$govt == 1])
sum(agg_other$other[agg_other$govt == 0])
sum(agg_other$other[agg_other$govt == 1])*100/nrow(shame[which(shame$govt==1),])
sum(agg_other$other[agg_other$govt == 0])*100/nrow(shame[which(shame$govt==0),])
t.test(agg_other$other[which(agg_other$govt == 0)],
       agg_other$other[which(agg_other$govt == 1)])

#Descriptive: Who is the *first* state to name and shame a rebel group? -----
first_shame = shame %>% 
  filter(condemned.ucdp.id>159) %>% 
  filter(first_entry_dummy==1) %>% 
  group_by(country)  %>% 
  summarise(sum(any_member )) %>% 
  rename(num_first_moves = `sum(any_member)`)%>% 
  filter(num_first_moves>0) %>% 
  mutate(country = case_when(
    country == "United Kingdom Of Great Britain And Northern Ireland" ~ "United Kingdom",
    country == "Russian Federation" ~ "Russia",
    country == "United States Of America" ~ "United States",
    country == "Venezuela (Bolivarian Republic Of)" ~ "Venezuela",
    country == "United Republic Of Tanzania" ~ "Tanzania",
    country == "Republic Of Korea" ~ "South Korea",
    country == "Slovak Republic" ~ "Slovakia",
    country == "Syrian Arab Republic" ~ "Syria",
    # This last line is the default: if no conditions above are met,
    # keep the original country name.
    TRUE ~ country
  ))

jpeg(
  "/Users/cbrandt/Dropbox/AAA_Projects/UNSC/R code/replication_data/output/first_shaming_state.jpg",
  res = 300,       # Set the resolution for good quality
  width = 100,      # This controls the horizontal space (length of bars)
  height = 225,    # INCREASE THIS VALUE to add more space between the vertical country labels
  units = "mm",   
)

ggplot(data=first_shame, aes(x= reorder(country, num_first_moves), y=num_first_moves)) +
  geom_bar(stat="identity")+ coord_flip() + theme_minimal() +
  theme(axis.text=element_text(size=14),
        axis.title=element_text(size=10),
        axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +  # Add right margin
  xlab("Country") + ylab("Number of Times State was First\nto Name and Shame a NSAG")
dev.off()

#What percentage of condemnations condemned only one side of a conflict?
agg_by_speech2 = aggregate(shame$any_member, by=list(shame$condemned.ucdp.id, shame$both.sides.dummy, shame$doc.id), FUN=sum)
colnames(agg_by_speech2) = c("condemned.ucdp.id", "both.sides.dummy", "doc.id", "x")

numerator = nrow(agg_by_speech2[which(agg_by_speech2$both.sides.dummy==0),])
(numerator/nrow(agg_by_speech2))*100

shame_aggregated <- shame %>%
  group_by(gwno_a, condemned.ucdp.id, doc.id, year) %>%
  summarise(               # Count of shaming events
    shamed_dummy = 1                   # Since at least one shaming event occurred                 
  ) %>%
  ungroup()
shame_aggregated$govt = ifelse(shame_aggregated$condemned.ucdp.id<160,1, 0) 


##Histogram of shamed actors -------
#Aggregating the data such that 
#We transform the data above to create our dependent variable Naming and Shaming. 
#Naming and Shaming is a dummy variable that indicates whether a UNSC member 
#names and shames a NSAG, for any type of behavior, in a given year. 
#Multiple occurrences of condemnations within a year are consolidated into a single binary indicator. 
agg_for_hist = aggregate(x=shame_aggregated$shamed_dummy, by=list(shame_aggregated$condemned.ucdp.id, shame_aggregated$doc.id), FUN=sum, na.rm=TRUE) 
agg_for_hist$one = 1
agg_for_hist$govt = ifelse(agg_for_hist$Group.1<160, 1, 0)
agg_for_hist$nsag = ifelse(agg_for_hist$Group.1>159, 1, 0)
agg_for_hist_nsag = agg_for_hist[which(agg_for_hist$Group.1>=160),]
#How many unique non-state armed groups are there?
length(unique(agg_for_hist_nsag$Group.1)) 

#how many condemnations are there?
nrow(agg_for_hist)
sum(agg_for_hist$govt)
sum(agg_for_hist$nsag)

#histogram of which NSAGs are named and shamed 
agg_for_hist_nsag = aggregate(x=agg_for_hist_nsag$one, by=list(agg_for_hist_nsag$Group.1), FUN=sum, na.rm=TRUE) 
colnames(agg_for_hist_nsag)[1] = "ActorId"
agg_for_hist_nsag = merge(agg_for_hist_nsag, actor_id, by="ActorId", all.x=TRUE, all.y=FALSE)
agg_for_hist_nsag = agg_for_hist_nsag[order(-agg_for_hist_nsag$x),]
head(agg_for_hist_nsag)
agg_for_hist_nsag$NameData[which(agg_for_hist_nsag$NameData=="Jama'atu Ahlis Sunna Lidda'awati wal-Jihad")] = "Boko Haram"
#See footnote on the exclusion of un-named groups 
agg_for_hist_nsag = agg_for_hist_nsag[which(agg_for_hist_nsag$x>0 &agg_for_hist_nsag$NameData!="NA"&
                                              agg_for_hist_nsag$NameData!="Palestinians insurgents"&agg_for_hist_nsag$NameData!="Syrian insurgents"),]


jpeg("/Users/cbrandt/Dropbox/AAA_Projects/UNSC/R code/replication_data/output/histogram_by_group.jpg", width = 540, height = 720)
ggplot(data=agg_for_hist_nsag, aes(x= reorder(NameData, x), y=x)) +  
  geom_bar(stat="identity", width=1)+ coord_flip() +   
  theme_minimal() + xlab("NSAG") + ylab("Number of Condemnations") 
dev.off()

agg_for_hist_nsag = agg_for_hist_nsag[1:10,]
agg_for_hist_nsag$NameData[which(agg_for_hist_nsag$NameData=="LRA")] = "Lord's Resistance Army (LRA)"
agg_for_hist_nsag$NameData[which(agg_for_hist_nsag$NameData=="RUF")] = "Revolutionary United Front (RUF)"
agg_for_hist_nsag$NameData[which(agg_for_hist_nsag$NameData=="PIJ")] = "Palestinian Islamic Jihad (PIJ)"
agg_for_hist_nsag$NameData[which(agg_for_hist_nsag$NameData=="IS")] = "Islamic State"
agg_for_hist_nsag$NameData[which(agg_for_hist_nsag$NameData=="al-Qaida")] = "Al-Qaeda"
agg_for_hist_nsag$NameData[which(agg_for_hist_nsag$NameData=="Jama'atu Ahlis Sunna Lidda'awati wal-Jihad")] = "Boko Haram"


jpeg("/Users/cbrandt/Dropbox/AAA_Projects/UNSC/R code/replication_data/output/short_histogram_by_group.jpg")
ggplot(data=agg_for_hist_nsag, aes(x= reorder(NameData, x), y=x)) +
  geom_bar(stat="identity")+ coord_flip() + theme_minimal() +
  theme(axis.text=element_text(size=12),
        axis.title=element_text(size=14),
        axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0))) +  # Add right margin
  xlab("Non-State Armed Group") + ylab("Number of Condemnations")
dev.off()


#histogram of which states name and shame NSAGs
agg_for_hist_nsag2 <- shame_aggregated %>%
  filter(condemned.ucdp.id > 160) %>% 
  mutate(one = 1) %>%
  group_by(gwno_a, doc.id) %>% 
  summarise(tmp = sum(one), .groups = "drop") %>% 
  group_by(gwno_a) %>% 
  summarise(shamed_sum = sum(tmp), .groups = "drop") %>%
  mutate(country = countrycode(gwno_a, "gwn", "country.name")) %>%  # add country name
  arrange(desc(shamed_sum))                              # order highest → lowest

jpeg("/Users/cbrandt/Dropbox/AAA_Projects/UNSC/R code/replication_data/output/histogram_by_state.jpg", width = 540, height = 720)
ggplot(agg_for_hist_nsag2, aes(y=reorder(country, shamed_sum), x=shamed_sum)) + scale_x_continuous(breaks = seq(0, 200, by = 25)) + theme_minimal() + 
  geom_bar(stat="identity") + xlab("Number of Condemnations")+
  ylab("UNSC Member State")  
dev.off()

#short histogram 
jpeg("/Users/cbrandt/Dropbox/AAA_Projects/UNSC/R code/replication_data/output/histogram_by_state_short.jpg")
# Create the bar chart
agg_for_hist_nsag2 %>%
  arrange(desc(shamed_sum)) %>%   # Sort by the 'x' value in descending order
  slice(1:10) %>%       
  ggplot(aes(y=reorder(country, shamed_sum), x=shamed_sum)) +
  geom_bar(stat = "identity") +  # Added fill color for better aesthetics
  xlab("Number of Condemnations") +
  ylab("UNSC Member States") +
  xlim(0, 100) +
  theme_minimal() +theme(axis.text=element_text(size=18),
                         axis.title=element_text(size=20))
dev.off()

####Histogram of states shaming states 
agg_for_hist_nsag2 <- shame_aggregated %>%
  filter(condemned.ucdp.id < 160) %>% 
  mutate(one = 1) %>%
  group_by(gwno_a, doc.id) %>% 
  summarise(tmp = sum(one), .groups = "drop") %>% 
  group_by(gwno_a) %>% 
  summarise(shamed_sum = sum(tmp), .groups = "drop") %>%
  mutate(country = countrycode(gwno_a, "gwn", "country.name")) %>%  # add country name
  arrange(desc(shamed_sum))                                      # order highest → lowest


jpeg("/Users/cbrandt/Dropbox/AAA_Projects/UNSC/R code/replication_data/output/histogram_by_state_govt.jpg", width = 540, height = 720)
ggplot(agg_for_hist_nsag2, aes(y=reorder(country, shamed_sum), x=shamed_sum)) + scale_x_continuous(breaks = seq(0, 200, by = 25)) + theme_minimal() + 
  geom_bar(stat="identity") + xlab("Number of Condemnations")+
  ylab("UNSC Member State")  
dev.off()


###Plot over times
plot_over_time = aggregate(shame_aggregated$shamed_dummy, by=list(shame_aggregated$govt, shame_aggregated$year), FUN=sum)
colnames(plot_over_time) = c("actor_type", "year", "num_condem")
plot_over_time$actor_type<- factor(plot_over_time$actor_type, levels=c(1, 0), 
                                   labels=c("State", "Non-State")) 

jpeg("/Users/cbrandt/Dropbox/AAA_Projects/UNSC/R code/replication_data/output/plot_over_time.jpg")
ggplot(data=plot_over_time, aes(x=year, y=num_condem, color=actor_type)) +
  geom_line(aes(color=actor_type,linetype = actor_type)) +
  geom_point(aes(color=actor_type)) +
  labs(x="Year", y = "Number of Condemnations", color="Actor Type") +
  theme_minimal() +
  scale_color_manual(name="Actor Type", values=c("State" = "grey80", "Non-State" = "grey3")) +  # Correct color mapping
  theme(axis.text=element_text(size=12),
        axis.title=element_text(size=14)) +
  scale_linetype_manual(values = c("State" = "dashed", "Non-State" = "solid"), name="Actor Type") +
  scale_x_continuous(
    breaks = seq(1994, 2016, by = 4),
    limits = c(1994, 2016)) +         scale_y_continuous(
      breaks = seq(0, 125, by = 25),
      limits = c(0, 125)
    )
dev.off()

